/**
 * 初始化proTechPoints详情对话框
 */
var ProtechTrainPointsInfoDlg = {
    protechTrainPointsInfoData : {}
};

/**
 * 清除数据
 */
ProtechTrainPointsInfoDlg.clearData = function() {
    this.protechTrainPointsInfoData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ProtechTrainPointsInfoDlg.set = function(key, val) {
    this.protechTrainPointsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ProtechTrainPointsInfoDlg.get = function(key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
ProtechTrainPointsInfoDlg.close = function() {
    parent.layer.close(window.parent.ProtechTrainPoints.layerIndex);
};

/**
 * 收集数据
 */
ProtechTrainPointsInfoDlg.collectData = function() {
    this
    .set('id')
    .set('empName')
    .set('empCode')
    .set('majorType')
    .set('majorLevelCode')
    .set('majorLevel')
    .set('majorName')
    .set('getType')
    .set('appraisalOrg')
    .set('approvalDoc')
    .set('certificateNum')
    .set('dispatchDate')
    .set('examineOrg')
    .set('year')
    .set('integral')
    .set('createDate')
    ;
};

/**
 * 提交添加
 */
ProtechTrainPointsInfoDlg.addSubmit = function() {
    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }
    if(!$("#year").val()){
        Feng.error("年度不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/protechTrainPoints/add", function(data){
        Feng.success("添加成功!");
        window.parent.ProtechTrainPoints.table.refresh();
        ProtechTrainPointsInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.protechTrainPointsInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
ProtechTrainPointsInfoDlg.editSubmit = function() {

    $('.form-horizontal').data('bootstrapValidator').validate();
    if(!$('.form-horizontal').data('bootstrapValidator').isValid()){
        return ;
    }
    if(!$("#year").val()){
        Feng.error("年度不能为空!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    if(!$("#empName").val()){
        Feng.error("员工不能为空!!");
        $("#ensure").removeAttr("disabled");
        return ;
    }
    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/protechTrainPoints/update", function(data){
        Feng.success("修改成功!");
        window.parent.ProtechTrainPoints.table.refresh();
        ProtechTrainPointsInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.protechTrainPointsInfoData);
    ajax.start();
};

/**
 * 规则计算
 */
/**
 * 学历change
 */
ProtechTrainPointsInfoDlg.majorLevel = function () {
    $('#majorLevelCode').change(function(){
        debugger
        var selected = $("#majorLevelCode option:selected");
        var education = selected.text();
        var integral = selected.attr("integral");
        //设置隐藏域
        $("#majorLevel").val(education);
        $("#integral").val(integral);
    });
};

/**
 * 上传提交
 */
ProtechTrainPointsInfoDlg.upload = function () {
    layui.use('upload', function(){
        var upload = layui.upload;
        //固定格式导入
        upload.render({
            elem: '#selectFile'
            ,url: '/protechTrainPoints/import'
            ,auto: false
            ,accept: "file"
            ,exts: "xls|xlsx"
            ,bindAction: '#fixImport'
            ,before: function () {
                layer.msg("正在读取文件，请稍后...")
                loadIndex = layer.load(2);
            }
            ,done: function(res){
                layer.close(loadIndex);
                if(res.code == 200){
                    layer.alert("导入成功! <br>" + res.message, {
                        icon: 1,
                        skin: 'layui-layer-molv'
                    });

                }else{
                    debugger;
                    if(res.data){
                        layer.open({
                            type: 2,
                            title: '重名数据处理',
                            shadeClose: true,
                            shade: 0.8,
                            area: ['820px', '620px'],
                            content: 'repeat', //iframe的url
                            btn: ['保存','取消'],
                            yes: function(index, layero){
                                var repeatData = window["layui-layer-iframe" + index].ProTeachPointRepeat.getContestDatas();
                                debugger
                                $.ajax({
                                    type: 'POST',
                                    url: Feng.ctxPath + "/protechTrainPoints/adds",
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(repeatData),
                                    success: function (res) {
                                        Feng.success("操作成功!");
                                    }
                                });
                                layer.close(index); //如果设定了yes回调，需进行手工关闭
                            }

                        });
                    }else{
                        layer.alert("导入取消! <br>" + res.message, {
                            icon: 2,
                            skin: 'layui-layer-molv'
                        });
                    }

                }
                window.parent.ProtechTrainPoints.table.refresh();

            }
        });

    });
};

/**
 * 提交校验
 */
ProtechTrainPointsInfoDlg.formValidator = function () {

    $('.form-horizontal').bootstrapValidator({
        message: '校验失败',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '员工姓名不能为空'
                    }
                }
            }
        }
    });
};

/**
 * 选择员工
 */
ProtechTrainPointsInfoDlg.selectEmp = function (obj) {
    var url = Feng.ctxPath +'/employee/select_list';
    if(obj){
        url = url + "?empName="+$("#empName").val();
    }
    layer.open({
        type: 2,
        area: ['800px', '500px'],
        fixed: false, //不固定
        maxmin: true,
        content: url,
        btn: ['确定'],
        yes: function(index, layero){
            var res = window["layui-layer-iframe" + index].Employee.callbackdata();
            $("#empName").val(res.empName);
            $("#empCode").val(res.empCode);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }
    });
};
ProtechTrainPointsInfoDlg.template = function(){
    $("#template").click(function () {
        window.open(Feng.ctxPath + "/static/excel/专业技术资格积分模板.xlsx","_parent");
    });
};

$(function() {
    ProtechTrainPointsInfoDlg.formValidator();
    ProtechTrainPointsInfoDlg.majorLevel();
    ProtechTrainPointsInfoDlg.upload();
    ProtechTrainPointsInfoDlg.template();
});
